{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第四周作业 对用户进行聚类\n",
    "\n",
    "\n",
    "根据用户的属性进行聚类（KMwans聚类）\n",
    "尝试 k=20，40，80，并计算各自CH_scores\n",
    "由于样本数目较多，建议使用MiniBatchKMeans\n",
    "\n",
    "用户描述信息 共有7维特征\n",
    "\n",
    "user_id\n",
    "locale:地区 语言\n",
    "birthyear：出生年\n",
    "gender：性别\n",
    "joinedAt：用户加入APP的时间 ISO_8601 UTC time\n",
    "location：地点\n",
    "timezone：时区\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "#特征编码\n",
    "##from utils import FeatureEng\n",
    "\n",
    "from sklearn.preprocessing import normalize\n",
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>locale</th>\n",
       "      <th>birthyear</th>\n",
       "      <th>gender</th>\n",
       "      <th>joinedAt</th>\n",
       "      <th>location</th>\n",
       "      <th>timezone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3197468391</td>\n",
       "      <td>id_ID</td>\n",
       "      <td>1993</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-10-02T06:40:55.524Z</td>\n",
       "      <td>Medan  Indonesia</td>\n",
       "      <td>480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3537982273</td>\n",
       "      <td>id_ID</td>\n",
       "      <td>1992</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-09-29T18:03:12.111Z</td>\n",
       "      <td>Medan  Indonesia</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>823183725</td>\n",
       "      <td>en_US</td>\n",
       "      <td>1975</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-10-06T03:14:07.149Z</td>\n",
       "      <td>Stratford  Ontario</td>\n",
       "      <td>-240.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1872223848</td>\n",
       "      <td>en_US</td>\n",
       "      <td>1991</td>\n",
       "      <td>female</td>\n",
       "      <td>2012-11-04T08:59:43.783Z</td>\n",
       "      <td>Tehran  Iran</td>\n",
       "      <td>210.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3429017717</td>\n",
       "      <td>id_ID</td>\n",
       "      <td>1995</td>\n",
       "      <td>female</td>\n",
       "      <td>2012-09-10T16:06:53.132Z</td>\n",
       "      <td>NaN</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      user_id locale birthyear  gender                  joinedAt  \\\n",
       "0  3197468391  id_ID      1993    male  2012-10-02T06:40:55.524Z   \n",
       "1  3537982273  id_ID      1992    male  2012-09-29T18:03:12.111Z   \n",
       "2   823183725  en_US      1975    male  2012-10-06T03:14:07.149Z   \n",
       "3  1872223848  en_US      1991  female  2012-11-04T08:59:43.783Z   \n",
       "4  3429017717  id_ID      1995  female  2012-09-10T16:06:53.132Z   \n",
       "\n",
       "             location  timezone  \n",
       "0    Medan  Indonesia     480.0  \n",
       "1    Medan  Indonesia     420.0  \n",
       "2  Stratford  Ontario    -240.0  \n",
       "3        Tehran  Iran     210.0  \n",
       "4                 NaN     420.0  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users=pd.read_csv(\"users.csv\")\n",
    "users.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 38209 entries, 0 to 38208\n",
      "Data columns (total 7 columns):\n",
      "user_id      38209 non-null int64\n",
      "locale       38209 non-null object\n",
      "birthyear    38209 non-null object\n",
      "gender       38100 non-null object\n",
      "joinedAt     38152 non-null object\n",
      "location     32745 non-null object\n",
      "timezone     37773 non-null float64\n",
      "dtypes: float64(1), int64(1), object(5)\n",
      "memory usage: 2.0+ MB\n"
     ]
    }
   ],
   "source": [
    "users.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "后面四类有缺失值，所以要做缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "locale属性的不同取值和出现的次数\n",
      "\n",
      "en_US    17073\n",
      "id_ID    11817\n",
      "es_LA     1999\n",
      "en_GB     1745\n",
      "es_ES      981\n",
      "fa_IR      676\n",
      "ar_AR      584\n",
      "hu_HU      544\n",
      "fr_FR      529\n",
      "pt_BR      472\n",
      "ka_GE      407\n",
      "zh_CN      183\n",
      "ru_RU      135\n",
      "ja_JP      121\n",
      "de_DE      119\n",
      "tr_TR      109\n",
      "ko_KR       91\n",
      "it_IT       78\n",
      "vi_VN       61\n",
      "fr_CA       49\n",
      "zh_TW       41\n",
      "pt_PT       36\n",
      "th_TH       27\n",
      "km_KH       25\n",
      "pl_PL       24\n",
      "jv_ID       23\n",
      "cs_CZ       22\n",
      "sv_SE       22\n",
      "el_GR       19\n",
      "zh_HK       19\n",
      "         ...  \n",
      "bg_BG       11\n",
      "hr_HR       11\n",
      "nl_NL       10\n",
      "he_IL        9\n",
      "sk_SK        7\n",
      "sr_RS        6\n",
      "en_IN        5\n",
      "da_DK        4\n",
      "nb_NO        4\n",
      "mk_MK        4\n",
      "fi_FI        4\n",
      "ca_ES        4\n",
      "bn_IN        4\n",
      "bs_BA        3\n",
      "mn_MN        3\n",
      "az_AZ        2\n",
      "af_ZA        2\n",
      "fb_LT        2\n",
      "uk_UA        2\n",
      "en_UD        2\n",
      "lt_LT        2\n",
      "ku_TR        2\n",
      "lv_LV        2\n",
      "hi_IN        1\n",
      "cy_GB        1\n",
      "es_MX        1\n",
      "tl_PH        1\n",
      "pa_IN        1\n",
      "eo_EO        1\n",
      "et_EE        1\n",
      "Name: locale, Length: 64, dtype: int64\n",
      "\n",
      "location属性的不同取值和出现的次数\n",
      "\n",
      "Medan  Indonesia                      4509\n",
      "Yogyakarta                            3092\n",
      "Phnom Penh                            2169\n",
      "Los Angeles  California               1555\n",
      "                                      1475\n",
      "Santo Domingo  Dominican Republic     1442\n",
      "Toronto  Ontario                       696\n",
      "Phnom Penh  11                         631\n",
      "Tbilisi  Georgia                       540\n",
      "Phnom Pen  Phnum Penh  Cambodia        471\n",
      "San Francisco  California              434\n",
      "Jogjakarta  Indonesia                  418\n",
      "Djokja  Yogyakarta  Indonesia          398\n",
      "Jakarta  Indonesia                     394\n",
      "Jakarta  04                            293\n",
      "Los Angeles  CA                        220\n",
      "Bekasi                                 211\n",
      "Medan  26                              211\n",
      "Torrance  CA                           193\n",
      "undefined  undefined                   191\n",
      "Bandung  Indonesia                     179\n",
      "Miskolc  Hungary                       173\n",
      "Porto Alegre                           159\n",
      "Santo Domingo  05                      155\n",
      "Purwokerto  Jawa Tengah  Indonesia     154\n",
      "Surabaya  Indonesia                    154\n",
      "Ottawa  Ontario                        149\n",
      "New York  New York                     140\n",
      "Jombang  Jawa Timur  Indonesia         131\n",
      "Phoenix  Arizona                       128\n",
      "                                      ... \n",
      "Tunis Mills  Maryland                    1\n",
      "Swartz Creek  Michigan                   1\n",
      "Akungba  Ondo  Nigeria                   1\n",
      "Yantai  25                               1\n",
      "Malipampang  Bulacan  Philippines        1\n",
      "Loma Linda  CA                           1\n",
      "Solingen                                 1\n",
      "Rexdale  Ontario                         1\n",
      "Misurata  58                             1\n",
      "Borjomi                                  1\n",
      "Bakrajo  05                              1\n",
      "Batna  03                                1\n",
      "Barahona  Dominican Republic             1\n",
      "Bozeman  Montana                         1\n",
      "East Palo Alto  California               1\n",
      "Akbouch  Bejaia  Algeria                 1\n",
      "Los Guayacanes  Narino  Colombia         1\n",
      "Tuguegaro  Cagayan  Philippines          1\n",
      "Anjatan  Jawa Barat  Indonesia           1\n",
      "Apopka  FL                               1\n",
      "Matawan  New Jersey                      1\n",
      "Chetumal  23                             1\n",
      "Penang  09                               1\n",
      "Rupea                                    1\n",
      "Taunton  Somerset                        1\n",
      "Lawndale  CA                             1\n",
      "Purwodadi                                1\n",
      "Nouvelle France  14                      1\n",
      "Sukkur  05                               1\n",
      "Matane  Quebec                           1\n",
      "Name: location, Length: 2804, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#查看各个类别变量取值情况\n",
    "var=['locale','location']\n",
    "for v in var:\n",
    "    print ('\\n%s属性的不同取值和出现的次数\\n'%v)\n",
    "    print (users[v].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>locale</th>\n",
       "      <th>birthyear</th>\n",
       "      <th>gender</th>\n",
       "      <th>joinedAt</th>\n",
       "      <th>timezone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>id_ID</td>\n",
       "      <td>1993</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-10-02T06:40:55.524Z</td>\n",
       "      <td>480.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>id_ID</td>\n",
       "      <td>1992</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-09-29T18:03:12.111Z</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>en_US</td>\n",
       "      <td>1975</td>\n",
       "      <td>male</td>\n",
       "      <td>2012-10-06T03:14:07.149Z</td>\n",
       "      <td>-240.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>en_US</td>\n",
       "      <td>1991</td>\n",
       "      <td>female</td>\n",
       "      <td>2012-11-04T08:59:43.783Z</td>\n",
       "      <td>210.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>id_ID</td>\n",
       "      <td>1995</td>\n",
       "      <td>female</td>\n",
       "      <td>2012-09-10T16:06:53.132Z</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  locale birthyear  gender                  joinedAt  timezone\n",
       "0  id_ID      1993    male  2012-10-02T06:40:55.524Z     480.0\n",
       "1  id_ID      1992    male  2012-09-29T18:03:12.111Z     420.0\n",
       "2  en_US      1975    male  2012-10-06T03:14:07.149Z    -240.0\n",
       "3  en_US      1991  female  2012-11-04T08:59:43.783Z     210.0\n",
       "4  id_ID      1995  female  2012-09-10T16:06:53.132Z     420.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#user_id不作为聚类属性\n",
    "users = users.drop([\"user_id\"], axis=1)\n",
    "        \n",
    "#location有缺失值，粗暴抛弃\n",
    "#也可以将缺失值作为另外一类：others\n",
    "users = users.drop([\"location\"], axis=1)\n",
    "users.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#特征编码\n",
    "import datetime\n",
    "import hashlib\n",
    "import locale\n",
    "\n",
    "from collections import defaultdict\n",
    "from sklearn.preprocessing import normalize\n",
    "\n",
    "class FeatureEng:\n",
    "    def __init__(self):\n",
    "         # 载入 locales\n",
    "        self.localeIdMap = defaultdict(int)\n",
    "        for i, l in enumerate(locale.locale_alias.keys()):\n",
    "          self.localeIdMap[l] = i + 1\n",
    "        \n",
    "        # 载入 gender id 字典\n",
    "        ##缺失补0\n",
    "        self.genderIdMap = defaultdict(int, {'NaN': 0, \"male\":1, \"female\":2})\n",
    "\n",
    "  \n",
    "    def getLocaleId(self, locstr):\n",
    "        return self.localeIdMap[locstr.lower()]\n",
    "\n",
    "    def getGenderId(self, genderStr):\n",
    "        return self.genderIdMap[genderStr]\n",
    "\n",
    "    def getJoinedYearMonth(self, dateString):\n",
    "        try:\n",
    "            dttm = datetime.datetime.strptime(dateString, \"%Y-%m-%dT%H:%M:%S.%fZ\")\n",
    "            #return \"\".join([str(dttm.year), str(dttm.month)])\n",
    "            return (dttm.year-2010)*12 + dttm.month\n",
    "        except:  #缺失补0\n",
    "          return 0\n",
    "\n",
    "    def getBirthYearInt(self, birthYear):\n",
    "        #缺失补0\n",
    "        try:\n",
    "          return 0 if birthYear == \"None\" else int(birthYear)\n",
    "        except:\n",
    "          return 0\n",
    "\n",
    "    def getTimezoneInt(self, timezone):\n",
    "        try:\n",
    "          return int(timezone)\n",
    "        except:  #缺失值处理\n",
    "          return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "FE=FeatureEng()\n",
    "cols= ['LocaleId','BirthYearInt','GenderId','JoinedYearMonth','timezone']\n",
    "n_cols = len(cols)\n",
    "n_users = users.shape[0]\n",
    "userMatrix = np.zeros((n_users,n_cols),dtype=np.int)\n",
    "\n",
    "for i in range(n_users): \n",
    "    userMatrix[i, 0] = FE.getLocaleId(users.loc[i,'locale'])\n",
    "    userMatrix[i, 1] = FE.getBirthYearInt(users.loc[i,'birthyear'])\n",
    "    userMatrix[i, 2] = FE.getGenderId(users.loc[i,'gender'])\n",
    "    userMatrix[i, 3] = FE.getJoinedYearMonth(users.loc[i,'joinedAt'])\n",
    "    #userMatrix[i, 4] = FE.getCountryId(df[''])\n",
    "    userMatrix[i, 4] = FE.getTimezoneInt(users.loc[i,'timezone'])\n",
    "\n",
    "# 归一化用户矩阵\n",
    "userMatrix = normalize(userMatrix, norm=\"l1\", axis=0, copy=False)\n",
    "\n",
    "dfdata = pd.DataFrame(data=userMatrix, columns=cols)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LocaleId</th>\n",
       "      <th>BirthYearInt</th>\n",
       "      <th>GenderId</th>\n",
       "      <th>JoinedYearMonth</th>\n",
       "      <th>timezone</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>-0.000018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000038</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000038</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000031</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   LocaleId  BirthYearInt  GenderId  JoinedYearMonth  timezone\n",
       "0  0.000036      0.000027  0.000019         0.000026  0.000036\n",
       "1  0.000036      0.000027  0.000019         0.000026  0.000031\n",
       "2  0.000020      0.000027  0.000019         0.000026 -0.000018\n",
       "3  0.000020      0.000027  0.000038         0.000027  0.000016\n",
       "4  0.000036      0.000027  0.000038         0.000026  0.000031"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import MiniBatchKMeans\n",
    "\n",
    "#一个参数点（聚类数据为K）的模型，在校验集上评价聚类算法性能\n",
    "\n",
    "def K_cluster_analysis(K,df):\n",
    "    print(\"K-means begin with clusters :{}\".format(K));\n",
    "    #k-means,在训练集上训练\n",
    "    km= MiniBatchKMeans(n_clusters =K)\n",
    "    km.fit(df)\n",
    "    #保存预测结果\n",
    "    col_name = \"cluster_\" + str(K)\n",
    "    df[col_name] = km.predict(df)\n",
    "    #k值的评估标准\n",
    "    #常见的方法有轮廓系数 Silhouette Coefficient 和 Calinski— Harabasz Index\n",
    "    #这两种分数值越大则聚类效果越好\n",
    "    CH_score =metrics.silhouette_score(df,df[col_name])\n",
    "    print (\"CH_score:{}\".format(CH_score))\n",
    "    return CH_score\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K-means begin with clusters :20\n",
      "CH_score:0.7446246063200127\n",
      "K-means begin with clusters :40\n",
      "CH_score:0.9012792898287443\n",
      "K-means begin with clusters :80\n",
      "CH_score:0.9977522483198104\n"
     ]
    }
   ],
   "source": [
    "#设置超参数（聚类数目K）搜索范围\n",
    "Ks = [20,40,80]\n",
    "CH_scores = []\n",
    "for K in Ks:\n",
    "    ch = K_cluster_analysis(K,dfdata)\n",
    "    CH_scores.append(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LocaleId</th>\n",
       "      <th>BirthYearInt</th>\n",
       "      <th>GenderId</th>\n",
       "      <th>JoinedYearMonth</th>\n",
       "      <th>timezone</th>\n",
       "      <th>cluster_20</th>\n",
       "      <th>cluster_40</th>\n",
       "      <th>cluster_80</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000036</td>\n",
       "      <td>13</td>\n",
       "      <td>12</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000031</td>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000019</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>-0.000018</td>\n",
       "      <td>16</td>\n",
       "      <td>19</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000038</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000016</td>\n",
       "      <td>11</td>\n",
       "      <td>18</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.000036</td>\n",
       "      <td>0.000027</td>\n",
       "      <td>0.000038</td>\n",
       "      <td>0.000026</td>\n",
       "      <td>0.000031</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   LocaleId  BirthYearInt  GenderId  JoinedYearMonth  timezone  cluster_20  \\\n",
       "0  0.000036      0.000027  0.000019         0.000026  0.000036          13   \n",
       "1  0.000036      0.000027  0.000019         0.000026  0.000031           8   \n",
       "2  0.000020      0.000027  0.000019         0.000026 -0.000018          16   \n",
       "3  0.000020      0.000027  0.000038         0.000027  0.000016          11   \n",
       "4  0.000036      0.000027  0.000038         0.000026  0.000031           2   \n",
       "\n",
       "   cluster_40  cluster_80  \n",
       "0          12          52  \n",
       "1           6           4  \n",
       "2          19          27  \n",
       "3          18          37  \n",
       "4           1          25  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfdata.to_csv('users_CR.csv')\n",
    "dfdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.74462460632001271, 0.90127928982874428, 0.99775224831981035]\n"
     ]
    }
   ],
   "source": [
    "print (CH_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1a1d4a9a58>]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xl4VPXd/vH3h7AjOwGRHUUE1ICm\nuFFxAUUoAQSBCDKoLdar+Dwu9Sn61OpPq9VWa2vro1JrHUBZRQiKCxShlaoQlIBE2RECKJFFkDXL\n5/fHjDRFNAOEnJnM/bquXMw5c4a5v2TmzvCdM9+YuyMiIsmhUtABRESk/Kj0RUSSiEpfRCSJqPRF\nRJKISl9EJImo9EVEkohKX0Qkiaj0RUSS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84G/RabfnzawWiTmWkoYCE6OXE2os7r4ZeBzYCGwFvgKWUA7PlYQvfTM7BXgF\nuN3ddwed53i5e5FH/svaHOgKdDjaYeWb6tiY2Y+Abe6+pOTuoxwa1+Mo4RJ3Pw+4hsj04aVBBzpO\nlYHzgGfcvQuwlzif/ihNdK47A5gadJbjEX3PoR/QBjgNqEXkcXakMn+uJHTpm1kVIoX/krtPj+7+\nwsyaRq9vSuSVc8Jw913AfCLvU9Qzs29+eX1zYEtQuWJ0CZBhZhuASUSmeP5A4o0DAHffEv1zG5F5\n464k5uMrD8hz9w+i29OI/BBIxLF84xrgQ3f/IrqdaGPpAax393x3LwCmAxdTDs+VhC19MzPgr8An\n7v77EldlAaHo5RCRuf64ZmapZlYverkGkQfEJ8A7wKDoYXE/Fne/x92bu3trIv/1nufuw0iwcQCY\nWS0zq/3NZSLzxx+TgI8vd/8c2GRm7aO7rgRyScCxlJDJv6d2IPHGshG40MxqRrvsm+/JSX+uJOyH\ns8ysG/BPYDn/nj++l8i8/hSgJZF/2OvcfUcgIWNkZucCYSLv4FcCprj7g2bWlsgr5gbAR8Bwdz8Y\nXNLYmdllwM/d/UeJOI5o5lejm5WBl939YTNrSII9vgDMrDPwPFAVWAfcSPSxRuKNpSaRN0HbuvtX\n0X0J932Jnpo9hMiZiB8BPyYyh39SnysJW/oiInLsEnZ6R0REjp1KX0Qkiaj0RUSSiEpfRCSJqPRF\nRJKISl9EJImo9EVEkohKX0Qkifx/LOkpPrBllKwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1a19bbe940>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#绘制不同PCA维数下模型的性能，找到最佳模型/参数（分数最高）\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.plot(Ks,np.array(CH_scores),'g-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
